
<template>
  <el-dialog
    title="培训申请"
    :visible.sync="TrainFlowData.dialogVisible"
    class="mini"
    :modal-append-to-body="false"
  >
    <div style="max-height: 60vh">
      <el-form label-width="80px">
        <el-form-item label="申请原因">
          <el-input
            v-model.trim="temp.opinion"
            type="textarea"
            placeholder="请输入内容"
            maxlength="200"
            show-word-limit
            :disabled="true"
          />
        </el-form-item>
        <el-form-item v-if="temp.handleUserName" label="申请人" prop="name">
          <el-tag>
            {{ temp.handleUserName }}
          </el-tag>
        </el-form-item>
        <!-- 1 -->
        <el-form-item label="审批意见">
          <el-input
            v-model.trim="opinion1"
            type="textarea"
            placeholder="请输入内容"
            maxlength="200"
            show-word-limit
            :disabled="schedule > 1"
          />
        </el-form-item>
        <el-form-item v-if="schedule >= 1" label="审批人" prop="name">
          <el-tag
            v-if="temp1.handleUserName"
            :disable-transitions="false"
            class="tag"
          >
            {{ temp1.handleUserName }}
          </el-tag>
          <el-button
            v-if="schedule == 1"
            size="mini"
            round
            circle
            icon="el-icon-plus"
            @click="openReview(3)"
          />
        </el-form-item>
        <!-- 2 -->
        <el-form-item v-if="schedule >= '2'" label="审批意见">
          <el-input
            v-model.trim="opinion2"
            type="textarea"
            placeholder="请输入内容"
            maxlength="200"
            show-word-limit
            :disabled="schedule > 2"
          />
        </el-form-item>
        <el-form-item v-if="schedule >= '2'" label="审批人" prop="name">
          <el-tag
            v-if="temp2.handleUserName"
            :disable-transitions="false"
            class="tag"
          >
            {{ temp2.handleUserName }}
          </el-tag>
          <el-button
            v-if="schedule == 2"
            size="mini"
            round
            circle
            icon="el-icon-plus"
            @click="openReview(3)"
          />
        </el-form-item>
        <!-- 3 -->
        <el-form-item v-if="schedule >= '3'" label="审批意见">
          <el-input
            v-model.trim="opinion3"
            type="textarea"
            placeholder="请输入内容"
            maxlength="200"
            show-word-limit
          />
        </el-form-item>

        <el-form-item label="处理结果">
          <el-radio v-model="radio" label="1">同意</el-radio>
          <el-radio v-model="radio" label="2">拒绝</el-radio>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="TrainFlowData.dialogVisible = false">取 消</el-button>
      <el-button @click="LookTrain">预览</el-button>
      <el-button type="primary" @click="cancelApply">提交</el-button>
    </span>
    <SearchPersonnel
      :search-personnel-data="SearchPersonnelData"
      @dataListData="dataListData"
    />
    <el-dialog
      title="查看培训"
      :visible.sync="LookTraindialog"
      width="60%"
      :modal-append-to-body="false"
      append-to-body
    >
      <TrainPaper :props-data="propsData" @emitData="emitData" />
    </el-dialog>
  </el-dialog>
</template>

<script>
import SearchPersonnel from '@/components/SearchUser/SearchPersonnel.vue'
import { Audit1, Audit2, Audit3 } from '@/api/ts/train.js'
import TrainPaper from '@/views/train/components/TrainPaper/index.vue'

export default {
  components: {
    SearchPersonnel,
    TrainPaper
  },
  props: ['TrainFlowData'],
  data() {
    return {
      radio: '1',
      temp: {},
      temp1: {},
      temp2: {},
      temp3: {},
      reason: '',
      SearchPersonnelData: {},
      opinion1: '',
      opinion2: '',
      opinion3: '',
      schedule: '',
      LookTraindialog: false,
      propsData: {}
    }
  },
  watch: {
    TrainFlowData(data) {
      this.temp = data.row
      this.temp.map((item) => {
        if (item.phase.indexOf('newApply') != -1) {
          this.temp = item
          this.schedule = '1'
          this.opinion = item.opinion
        } else if (item.phase.indexOf('audit1') != -1) {
          this.schedule = '2'
          this.temp1 = item
          this.opinion1 = item.opinion
        } else if (item.phase.indexOf('audit2') != -1) {
          this.schedule = '3'
          this.temp2 = item
          this.opinion2 = item.opinion
        }
      })
    }
  },
  methods: {
    cancelApply() {
      console.log(this.schedule)
      if (this.schedule == 1) {
        const data = {
          trainId: this.temp.trainId,
          opinion: this.opinion1,
          result: this.radio,
          nextHandleUserId: this.temp1.reviewId
        }
        if (data.opinion == '') {
          this.$message.warning('审批意见不可为空')
          return
        }
        if (data.result == null) {
          this.$message.warning('处理结果不可为空')
          return
        }
        Audit1(data).then((res) => {
          if (res.code) {
            this.TrainFlowData.dialogVisible = false
            this.$message.success('提交成功')
            this.$emit('getList')
            this.$forceUpdate()
          }
        })
      } else if (this.schedule == 2) {
        const data = {
          trainId: this.temp.trainId,
          opinion: this.opinion2,
          result: this.radio,
          nextHandleUserId: this.temp2.reviewId
        }
        if (data.opinion == '') {
          this.$message.warning('审批意见不可为空')
          return
        }
        if (data.result == null) {
          this.$message.warning('处理结果不可为空')
          return
        }
        Audit2(data).then((res) => {
          if (res.code) {
            this.TrainFlowData.dialogVisible = false
            this.$message.success('提交成功')
            this.$emit('getList')
            this.$forceUpdate()
          }
        })
      } else if (this.schedule == 3) {
        const data = {
          trainId: this.temp.trainId,
          opinion: this.opinion3,
          result: this.radio
          // nextHandleUserId: this.temp3.reviewId
        }
        if (data.opinion == '') {
          this.$message.warning('审批意见不可为空')
          return
        }
        if (data.result == null) {
          this.$message.warning('处理结果不可为空')
          return
        }
        Audit3(data).then((res) => {
          if (res.code) {
            this.TrainFlowData.dialogVisible = false
            this.$message.success('提交成功')
            this.$emit('getList')
            this.$forceUpdate()
          }
        })
      } else {
        return
      }
    },
    dataListData(data) {
      if (data) {
        if (this.schedule == 1) {
          this.temp1.handleUserName = data[0].label
          this.temp1.reviewId = data[0].id
        } else if (this.schedule == 2) {
          this.temp2.handleUserName = data[0].label
          this.temp2.reviewId = data[0].id
        } else if (this.schedule == 3) {
          this.temp3.handleUserName = data[0].label
          this.temp3.reviewId = data[0].id
        }

        this.$forceUpdate()
      }
    },
    openReview(val, type, title, temp) {
      // 0为角色 1为角色（总经理，总监） 3为架构 4为架构多选 5部门多选
      // this.reviewWindow = val
      // if (val == 0) {
      //   this.SearchPersonnelTypeData = {
      //     val: val,
      //     type: type,
      //     dialogVisible: true,
      //     title: title,
      //     temp: this.temp
      //   }
      // } else if (val == 1) {
      //   if (
      //     this.temp.documentLevelNum == 1 ||
      //     this.temp.documentLevelNum == 2
      //   ) {
      //     type = 'president'
      //     title = '总经理'
      //   } else {
      //     type = 'director'
      //     title = '总监'
      //   }
      //   this.SearchPersonnelTypeData = {
      //     val: val,
      //     type: type,
      //     dialogVisible: true,
      //     title: title
      //   }
      // } else
      if (val == 3) {
        this.SearchPersonnelData = {
          dialogVisible: true
        }
      }
      // else if (val == 4) {
      //   this.SearchUserListData = {
      //     dialogVisible: true
      //   }
      // } else if (val == 5) {
      //   this.SelectGroupListData = {
      //     dialogVisible: true
      //   }
      // }
    },
    // 查看//编辑
    LookTrain() {
      this.LookTraindialog = true
      this.propsData = { type: 1, trainId: this.temp.trainId }
    },
    emitData(data) {
      if (data) {
        this.LookTraindialog = false
      }
    }
  }
}
</script>

<style>
</style>
